/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
  <file> uls_init.c </file>
  <brief>
    The initialization routines for ULS.
    This file is part of ULS, Unified Lexical Scheme.
  </brief>
  <author>
    Stanley J. Hong <babel96@gmail.com>, 2011.
  </author>
*/

#include "common.h"
#include "uls.h"
#include "uls_lex.h"
#include "uls_lf_sprintf.h"

int
uls_fmtproc_coord(uls_lf_context_t* ctx, uls_lf_puts_t puts_proc)
{
	uls_lex_t  *uls = (uls_lex_t *) ctx->g_dat;
	uls_lf_char_t buf[81];
	int len;

	if (uls == NULL) return 0;

	len = uls_snprintf(buf, sizeof(buf), "[%s:%d]",
		uls_get_tag(uls), uls_get_lineno(uls));
	puts_proc(ctx->x_dat, buf, len);

	return len;
}

int
uls_fmtproc_tokname(uls_lf_context_t* ctx, uls_lf_puts_t puts_proc)
{
	uls_lex_t  *uls = (uls_lex_t *) ctx->g_dat;
	uls_lf_char_t *wrdptr;
	int len;

	if (uls == NULL) return 0;
	if ((wrdptr=(uls_lf_char_t *) get_keyword_str(uls, uls_token_id(uls))) == NULL)
		wrdptr = "?";

	len = uls_strlen(wrdptr);
	puts_proc(ctx->x_dat, wrdptr, len);

	return len;
}

extern void initialize_csz(void);
extern void finalize_csz(void);
extern void initialize_syserr(void);
extern void finalize_syserr(void);
extern void initialize_sysprn(void);
extern void finalize_sysprn(void);
extern void initialize_uls_lf(void);
extern void finalize_uls_lf(void);
static int uls_initialized;

void
#ifdef __GNUC__
__attribute__((constructor))
#endif
__initialize_uls(void)
{
	srand((unsigned int) time(NULL));
	if (uls_initialized) return;

	initialize_csz();
	initialize_syserr();
	initialize_sysprn();
	initialize_uls_lf();

	uls_lf_register_convspec(NULL, "w", uls_fmtproc_coord);
	uls_lf_register_convspec(NULL, "k", uls_fmtproc_tokname);

	uls_initialized = 1;
}

void
#ifdef __GNUC__
__attribute__((destructor))
#endif
__finalize_uls(void)
{
	if (!uls_initialized) return;

	finalize_sysprn();
	finalize_syserr();

	finalize_uls_lf();
	finalize_csz();

	uls_initialized = 0;
}

void
initialize_uls(void)
{
	if (uls_initialized) return;
	__initialize_uls();
	atexit(__finalize_uls);
}

#ifdef WIN32
#ifdef WIN32DEBUG

#pragma section(".CRT$XCU", read) 
__declspec(allocate(".CRT$XCU")) static void
(__cdecl *__win_staticlib_ctor[])(void) = { __initialize_uls }; 

#pragma section(".CRT$XPU", read) 
__declspec(allocate(".CRT$XPU")) static void
(__cdecl *__win_staticlib_dtor[])(void) = { __finalize_uls }; 

#else
BOOL WINAPI
DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpvReserved)
{
	switch (dwReason) {
	case DLL_PROCESS_ATTACH:
		__initialize_uls();
		break;

	case DLL_THREAD_ATTACH:
		break;

	case DLL_THREAD_DETACH:
		break;

	case DLL_PROCESS_DETACH:
		__finalize_uls();
		break;
	}

	return TRUE;
}
#endif
#endif
